package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.service.AdminService;


@Controller
@RequestMapping(value = "/admin")
public class AdminApprovalLeave extends BaseController {

	@Autowired
	private AdminService adminService;
	private static Logger logger = LoggerFactory.getLogger(AdminApprovalLeave.class);
	
	/**
	 * 请假审批列表
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTeacherLeaveInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTeacherLeaveInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校Id
			String driverId = request.getParameter("driverId");
			if(driverId != null && !driverId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("imgUrl", Config.getImageAddress());
				response = adminService.getTeacherLeaveInfo(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getTeacherLeaveInfo:"+ e.toString());
			response.setReturnMsg(1,"获取请假审批列表失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 请假审批详情页
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTeacherLeaveDetail", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTeacherLeaveDetail(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 课程Id
			String leaveCourseIds = request.getParameter("leaveCourseIds");
			Map<String,Object> paramsMap = new HashMap<>();
			if(leaveCourseIds == null || leaveCourseIds.equals("")){
				response.setErrorcode(1);
				response.setMessage("系统无课程数据！");
				return response.toJSON();
			}
			paramsMap.put("leaveCourseIds", leaveCourseIds.split(","));
			response = adminService.getTeacherLeaveDetail(paramsMap,response);			
		} catch (Exception e) {
			logger.info("getTeacherLeaveDetail:"+ e.toString());
			response.setReturnMsg(1,"获取详情失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 取消课程
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/deleteCourse", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String deleteCourse(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 课程Id
			String courseId = request.getParameter("courseId");
			if(courseId != null && !courseId.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("courseId", courseId);
				response = adminService.deleteCourse(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("deleteCourse:"+ e.toString());
			response.setReturnMsg(1,"取消课程失败！");
		} finally {
			return response.toJSON();
		}
	}

	/**
	 * 审批请假
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateLeaveStatus", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateLeaveStatus(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 请假数据主键Id
			String leaveId = request.getParameter("leaveId");
			//审批状态（1同意 2拒绝）
			String checkState = request.getParameter("checkState");
			if(leaveId != null && !leaveId.isEmpty()
					&& checkState != null && !checkState.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("leaveId", leaveId);
				paramsMap.put("checkState", checkState);
				response = adminService.updateLeaveStatus(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("updateLeaveStatus:"+ e.toString());
			response.setReturnMsg(1,"审批失败！");
		} finally {
			return response.toJSON();
		}
	}



}
